package org.dromara.app.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.rong.messages.BaseMessage;
import io.rong.messages.TxtMessage;
import io.rong.messages.UserInfo;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.message.GroupMessage;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.codec.binary.Base64;
import org.dromara.app.appParams.*;
import org.dromara.app.appReturn.*;
import org.dromara.app.config.AppGoEasyUtil;
import org.dromara.app.config.AppRongYunUtil;
import org.dromara.app.cssUtils.CssExceptionDescription;
import org.dromara.app.cssUtils.CssUtils;
import org.dromara.app.domain.SkAppUser;
import org.dromara.app.domain.SkYueJu;
import org.dromara.app.mapper.SkAppUserMapper;
import org.dromara.app.mapper.SkYueJuMapper;
import org.dromara.app.service.IBfExperienceLogService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.junit.Assert;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkYueJuUserBo;
import org.dromara.app.domain.vo.SkYueJuUserVo;
import org.dromara.app.domain.SkYueJuUser;
import org.dromara.app.mapper.SkYueJuUserMapper;
import org.dromara.app.service.ISkYueJuUserService;
import org.springframework.web.socket.TextMessage;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;

import static kotlin.reflect.jvm.internal.impl.builtins.StandardNames.FqNames.list;

/**
 * 约局用户信息Service业务层处理
 *
 * @author Evacuation
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
public class SkYueJuUserServiceImpl implements ISkYueJuUserService {

    private final SkYueJuUserMapper baseMapper;

    private final SkYueJuMapper skYueJuMapper;

    private final AppRongYunUtil appRongYunUtil;

    private final IBfExperienceLogService iBfExperienceLogService;

    private final SkAppUserMapper skAppUserMapper;

    private final AppGoEasyUtil appGoEasyUtil;

    /**
     * 查询约局用户信息
     *
     * @param id 主键
     * @return 约局用户信息
     */
    @Override
    public SkYueJuUserVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询约局用户信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 约局用户信息分页列表
     */
    @Override
    public TableDataInfo<SkYueJuUserVo> queryPageList(SkYueJuUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkYueJuUser> lqw = buildQueryWrapper(bo);
        Page<SkYueJuUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的约局用户信息列表
     *
     * @param bo 查询条件
     * @return 约局用户信息列表
     */
    @Override
    public List<SkYueJuUserVo> queryList(SkYueJuUserBo bo) {
        LambdaQueryWrapper<SkYueJuUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkYueJuUser> buildQueryWrapper(SkYueJuUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkYueJuUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SkYueJuUser::getUserId, bo.getUserId());
        lqw.eq(bo.getState() != null, SkYueJuUser::getState, bo.getState());
        lqw.eq(bo.getYueJuId() != null, SkYueJuUser::getYueJuId, bo.getYueJuId());
        return lqw;
    }

    /**
     * 新增约局用户信息
     *
     * @param bo 约局用户信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkYueJuUserBo bo) {
        SkYueJuUser add = MapstructUtils.convert(bo, SkYueJuUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改约局用户信息
     *
     * @param bo 约局用户信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkYueJuUserBo bo) {
        SkYueJuUser update = MapstructUtils.convert(bo, SkYueJuUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkYueJuUser entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除约局用户信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 加入约局
     *
     * @param req
     */
    @Override
    public void joinYueJu(AppJoinYueJuParams params, HttpServletRequest req) {
        SkYueJu skYueJu = skYueJuMapper.selectById(params.getYuejuId());
        if (skYueJu.getState().longValue() == 0) {
            CssExceptionDescription.errorMsg(10007);
        }
        Long userId = CssUtils.getUserId(req);
        if (userId.longValue() == skYueJu.getUserId()) {
            CssExceptionDescription.errorMsg(10012);
        }
        //判断是否已满人
        Long usernumber = baseMapper.selectCount(
            new QueryWrapper<SkYueJuUser>().lambda()
                .eq(SkYueJuUser::getYueJuId, params.getYuejuId())
                .eq(SkYueJuUser::getState,1)
        );
        if (usernumber.longValue() >= skYueJu.getAllUserNumber()){
            CssExceptionDescription.errorMsg(10027);
        }

        Long number = baseMapper.selectCount(
            new QueryWrapper<SkYueJuUser>().lambda()
                .eq(SkYueJuUser::getUserId, userId)
                .eq(SkYueJuUser::getYueJuId, params.getYuejuId())
        );
        if (number == null || number.longValue() == 0) {
            SkYueJuUser sk = new SkYueJuUser();
            sk.setYueJuId(params.getYuejuId());
            sk.setUserId(userId);
            sk.setIsCencel(0L);
            sk.setIsExit(0L);
            sk.setState(0L);
            baseMapper.insert(sk);
        }
    }

    /**
     * 同意拒绝/约局
     */
    @Override
    public void activeYueJu(AppActiveYueJuParams params, HttpServletRequest req) throws Exception {

        SkYueJuUser skYueJuUser = baseMapper.selectOne(
            new QueryWrapper<SkYueJuUser>().lambda()
                .eq(SkYueJuUser::getUserId, params.getActiveUserId())
                .eq(SkYueJuUser::getYueJuId, params.getYuejuId())
                .last("limit 1")
        );
        if (skYueJuUser == null) {
            CssExceptionDescription.errorMsg(10004);
        }
        if (skYueJuUser.getIsCencel().longValue() != 0L) {
            CssExceptionDescription.errorMsg(10005);
        }
        if (skYueJuUser.getIsExit().longValue() != 0L) {
            CssExceptionDescription.errorMsg(10006);
        }
        SkYueJu yueju = skYueJuMapper.selectById(params.getYuejuId());
        if (params.getState().longValue()==1){
            //判断是否已满人
            Long usernumber = baseMapper.selectCount(
                new QueryWrapper<SkYueJuUser>().lambda()
                    .eq(SkYueJuUser::getYueJuId, params.getYuejuId())
                    .eq(SkYueJuUser::getState,1)
            );
            if (usernumber.longValue() >= yueju.getAllUserNumber()){
                CssExceptionDescription.errorMsg(10027);
            }
        }

        skYueJuUser.setState(params.getState());
        baseMapper.updateById(skYueJuUser);

        //同意加入
        if (params.getState().longValue() == 1) {
            GroupMember[] members = new GroupMember[]{new GroupMember().setId(skYueJuUser.getUserId() + "")};
            //进行加入
            GroupModel group = new GroupModel()
                .setId(yueju.getId() + "")
                .setMembers(members)
                .setName(yueju.getTitle() + "&|*&" + yueju.getImage().split(",")[0]);
            appRongYunUtil.join(group);
            SkAppUser user = skAppUserMapper.selectById(skYueJuUser.getUserId());
            // 群主发送欢迎语
            SkAppUser app = skAppUserMapper.selectById(yueju.getUserId());
            UserInfo userInfo = new UserInfo();
            userInfo.setId(yueju.getUserId() + "");
            userInfo.setName(app.getNickName());
            userInfo.setPortrait(app.getHeadImage());
            Map<Object, Object> map = new HashMap<>();
            Map<String, Object> userExtra = new HashMap<>();
            userExtra.put("name", app.getNickName());
            userExtra.put("avatar", app.getHeadImage());
            map.put(yueju.getUserId() + "", userExtra);
            Map<String, Object> groupExtra = new HashMap<>();
            groupExtra.put("name", yueju.getTitle());
            groupExtra.put("avatar", yueju.getImage());
            map.put(yueju.getId() + "", groupExtra);
            userInfo.setExtra(JSONUtil.toJsonStr(map));
            GroupMessage message = new GroupMessage()
                .setContent(new TxtMessage("欢迎"+user.getNickName()+"加入群组", "",userInfo))
                .setTargetId(new String[]{yueju.getId()+ ""})
                .setSenderId(yueju.getUserId() + "")
                .setIsIncludeSender(1)
                .setObjectName("RC:TxtMsg");
            appRongYunUtil.publish(message);
            // 自己参与组局  添加经验值
            String obej = iBfExperienceLogService.getKeyValue("自己参与组局");
            System.out.println(obej);
            if (obej == null) {
                obej = "0";
            }
            iBfExperienceLogService.addLog(skYueJuUser.getUserId(), obej, "自己参与组局");
            // 我的直推参与组局 添加经验值
            if (user.getYqUserId() != null) {
                String obej2 = iBfExperienceLogService.getKeyValue("我的直推参与组局");
                if (obej2 == null) {
                    obej2 = "0";
                }
                iBfExperienceLogService.addLog(user.getYqUserId(), obej2, "我的直推参与组局");
            }
        }
    }

    /**
     * 加入约局用户取消
     */
    @Override
    public void joinCencelYueJu(AppJoinYueJuParams params, HttpServletRequest req) throws Exception {
        Long userId = CssUtils.getUserId(req);
        SkYueJu skYueJu = skYueJuMapper.selectById(params.getYuejuId());
        if (userId.longValue() != skYueJu.getUserId().longValue()) {
            CssExceptionDescription.errorMsg(10013);
        }
        skYueJu.setState(0L);
        skYueJuMapper.updateById(skYueJu);
        SkYueJuUser sk = new SkYueJuUser();
        sk.setIsCencel(1L);
        baseMapper.update(sk,
            new QueryWrapper<SkYueJuUser>().lambda().eq(SkYueJuUser::getYueJuId, params.getYuejuId())
        );
        GroupMember[] members = {new GroupMember()};
        GroupMember groupMember = new GroupMember();
        groupMember.setGroupId(params.getYuejuId() + "");
        groupMember.setId(sk.getUserId() + "");
        members[0] = groupMember;
        GroupModel group = new GroupModel()
            .setId(params.getYuejuId() + "")
            .setMembers(members);
        appRongYunUtil.quit(group);
    }

    /**
     * 已加入约局，用户进行退出
     */
    @Override
    public void joinExitYueJu(AppJoinYueJuParams params, HttpServletRequest req) throws Exception {

        Long userId = CssUtils.getUserId(req);
        SkYueJuUser sk = baseMapper.selectOne(
            new QueryWrapper<SkYueJuUser>().lambda()
                .eq(SkYueJuUser::getUserId, userId)
                .eq(SkYueJuUser::getYueJuId, params.getYuejuId())
                .last("limit 1")
        );
        sk.setIsExit(1L);
        baseMapper.updateById(sk);
        GroupMember[] members = new GroupMember[]{new GroupMember().setId(sk.getUserId() + "")};
        GroupModel group = new GroupModel()
            .setId(params.getYuejuId() + "")
            .setMembers(members);
        appRongYunUtil.quit(group);
    }

    /**
     * 进行中的约局
     *
     * @param req
     * @return
     */
    @Override
    public List<AppZhengZaiZuJuReturn> getUserYueJuList(HttpServletRequest req) {
        Long userId = CssUtils.getUserId(req);
        List<AppZhengZaiZuJuReturn> list = baseMapper.getUserYueJuList(userId);
        return list;
    }

    /**
     * 约局群组列表
     *
     * @param req
     * @return
     */
    @Override
    public List<AppZhengZaiZuJuReturn> getGroupList(HttpServletRequest req) {
        Long userId = CssUtils.getUserId(req);
        List<AppZhengZaiZuJuReturn> list = baseMapper.getGroupList(userId);
        return list;
    }

    /**
     * 我发起的约局
     *
     * @param params
     * @param req
     * @return
     */
    @Override
    public List<AppZhengZaiZuJuReturn> getMyFaQiGroupList(AppPageParams params, HttpServletRequest req) {
        Long userId = CssUtils.getUserId(req);
        List<AppZhengZaiZuJuReturn> list = baseMapper.getMyFaQiGroupList(params, userId);
        for (AppZhengZaiZuJuReturn app : list) {
            if ((app.getState().longValue() == 2 || app.getState().longValue() == 1)
                && ((StringUtils.isNotEmpty(app.getUnlimited()) && app.getUnlimited().equals("1"))
                || (StringUtils.isNotEmpty(app.getEndTime()) && DateUtil.parse(app.getEndTime(), DatePattern.NORM_DATETIME_PATTERN).getTime() > new Date().getTime()))) {
                app.setIsCommon(0L);
            } else {
                app.setIsCommon(1L);
            }
        }
        return list;
    }

    /**
     * 我参与的约局
     *
     * @param params
     * @param req
     * @return
     */
    @Override
    public List<AppZhengZaiZuJuReturn> getMyCanYuGroupList(AppPageParams params, HttpServletRequest req) {
        Long userId = CssUtils.getUserId(req);
        List<AppZhengZaiZuJuReturn> list = baseMapper.getMyCanYuGroupList(params, userId);
        for (AppZhengZaiZuJuReturn app : list) {
            if (app.getState().longValue() == 1
                && ((StringUtils.isNotEmpty(app.getUnlimited()) && app.getUnlimited().equals("1"))
                || (StringUtils.isNotEmpty(app.getEndTime()) && DateUtil.parse(app.getEndTime(), DatePattern.NORM_DATETIME_PATTERN).getTime() > new Date().getTime()))) {
                app.setIsCommon(0L);
            } else {
                app.setIsCommon(1L);
            }
        }
        return list;
    }

    /**
     * 审核约局人(同意)
     *
     * @param req
     * @return
     */
    @Override
    public void getShengHeTy(AppDetailByIdParams params, HttpServletRequest req) throws Exception {
        SkYueJuUser skYueJuUser = baseMapper.selectById(params.getId());
        if (skYueJuUser == null) {
            CssExceptionDescription.errorMsg(10020);
        }
        SkYueJu yueju = skYueJuMapper.selectById(skYueJuUser.getYueJuId());
        if (yueju == null) {
            CssExceptionDescription.errorMsg(10020);
        }
        if (yueju.getState().longValue() == 0) {
            CssExceptionDescription.errorMsg(10021);
        }
        if (skYueJuUser.getState().longValue() == 3) {
            CssExceptionDescription.errorMsg(10022);
        }

            //判断是否已满人
        Long usernumber = baseMapper.selectCount(
            new QueryWrapper<SkYueJuUser>().lambda()
                .eq(SkYueJuUser::getYueJuId, skYueJuUser.getYueJuId())
                .eq(SkYueJuUser::getState,1)
        );
        if (usernumber.longValue() >= yueju.getAllUserNumber()){
            CssExceptionDescription.errorMsg(10027);
        }

        //判断是否已经满足要求人数
        Long newUserNumber = usernumber.longValue()+1;
        boolean isMz = false;
        if(newUserNumber.longValue() == yueju.getAllUserNumber().longValue()){
            yueju.setIsRm(1L);
            isMz = true;
        }
        skYueJuUser.setState(1L);
        skYueJuUser.setIsGroup(1L);
        baseMapper.updateById(skYueJuUser);
        //用户信息
        SkAppUser user = skAppUserMapper.selectById(skYueJuUser.getUserId());
        //未创建 进行创建群组
        if (yueju.getIsGroup().longValue() == 0) {
            List<SkYueJuUser> list = baseMapper.selectList(
                new QueryWrapper<SkYueJuUser>().lambda()
                    .eq(SkYueJuUser::getYueJuId, yueju.getId())
                    .eq(SkYueJuUser::getState, "1")
            );
            GroupMember[] members = {new GroupMember()};
            GroupModel group = new GroupModel()
                .setId(yueju.getId() + "")
                .setName(yueju.getTitle())
                .setContent(yueju.getContent())
                .setFromUserId(yueju.getUserId() + "")
                .setMaxMember(yueju.getAllUserNumber().intValue() + 1);
            if (list.size() >= 1 && yueju.getIsGroup().longValue() == 0) {
                //创建群组 GroupModel
                for (int i = 0; i < list.size(); i++) {
                    GroupMember groupMember = new GroupMember();
                    groupMember.setGroupId(yueju.getId() + "");
                    groupMember.setId(list.get(i).getUserId() + "");
                    members[i] = groupMember;
                }
                group.setMembers(members);
            }
            //发布人加入
            GroupMember groupMember = new GroupMember();
            groupMember.setGroupId(yueju.getId() + "");
            groupMember.setId(yueju.getUserId() + "");
            members[list.size()] = groupMember;
            appRongYunUtil.create(group);
            yueju.setIsGroup(1L);
            List<AppMemberReturn> listuser = new ArrayList<>();
            AppMemberReturn app = new AppMemberReturn();
            app.setUserId(yueju.getUserId());
            app.setHeadImage(user.getHeadImage());
            listuser.add(app);
            yueju.setGroupHeadImage(JSONUtil.toJsonStr(listuser));
            skYueJuMapper.updateById(yueju);
        } else {
            GroupMember[] members = {new GroupMember()};
            GroupMember groupMember = new GroupMember();
            groupMember.setGroupId(yueju.getId() + "");
            groupMember.setId(skYueJuUser.getUserId() + "");
            members[0] = groupMember;
            //已创建  进行加入
            GroupModel group = new GroupModel()
                .setId(yueju.getId() + "")
                .setMembers(members)
                .setName(yueju.getTitle());
            appRongYunUtil.join(group);
            if (isMz){
                skYueJuMapper.updateById(yueju);
            }

            List<AppMemberReturn> list = new ArrayList<>();
            if(yueju.getGroupHeadImage()!=null){
                list = JSONUtil.parseArray(yueju.getGroupHeadImage()).toList(AppMemberReturn.class);
            }
            AppMemberReturn app = new AppMemberReturn();
            app.setUserId(user.getId());
            app.setHeadImage(user.getHeadImage());
            list.add(app);
            yueju.setGroupHeadImage(JSONUtil.toJsonStr(list));
            skYueJuMapper.updateById(yueju);
        }
        // 群主发送欢迎语
        SkAppUser app = skAppUserMapper.selectById(yueju.getUserId());
        UserInfo userInfo = new UserInfo();
        userInfo.setId(yueju.getUserId() + "");
        userInfo.setName(app.getNickName());
        userInfo.setPortrait(app.getHeadImage());
        Map<Object, Object> map = new HashMap<>();
        Map<String, Object> userExtra = new HashMap<>();
        userExtra.put("name", app.getNickName());
        userExtra.put("avatar", app.getHeadImage());
        map.put(yueju.getUserId() + "", userExtra);
        Map<String, Object> groupExtra = new HashMap<>();
        groupExtra.put("name", yueju.getTitle());
        groupExtra.put("avatar", yueju.getImage());
        map.put(yueju.getId() + "", groupExtra);
        userInfo.setExtra(JSONUtil.toJsonStr(map));
        GroupMessage message = new GroupMessage()
            .setContent(new TxtMessage("欢迎"+user.getNickName()+"加入群组", "", userInfo))
            .setTargetId(new String[]{yueju.getId()+ ""})
            .setSenderId(yueju.getUserId() + "")
            .setIsIncludeSender(1)
            .setObjectName("RC:TxtMsg");
        appRongYunUtil.publish(message);
        // 自己参与组局  添加经验值
        String obej = iBfExperienceLogService.getKeyValue("自己参与组局");
        System.out.println(obej);
        if (obej == null) {
            obej = "0";
        }
        iBfExperienceLogService.addLog(skYueJuUser.getUserId(), obej, "自己参与组局");
        // 我的直推参与组局 添加经验值
//        SkAppUser user = skAppUserMapper.selectById(skYueJuUser.getUserId());
        if (user.getYqUserId() != null) {
            String obej2 = iBfExperienceLogService.getKeyValue("我的直推参与组局");
            if (obej2 == null) {
                obej2 = "0";
            }
            iBfExperienceLogService.addLog(user.getYqUserId(), obej2, "我的直推参与组局");
        }

    }

    /**
     * 审核约局人(拒绝)
     *
     * @param req
     * @return
     */
    @Override
    public void getShengHeJj(AppDetailByIdParams params, HttpServletRequest req) {
        SkYueJuUser skYueJuUser = baseMapper.selectById(params.getId());
        if (skYueJuUser == null) {
            CssExceptionDescription.errorMsg(10020);
        }
        SkYueJu skYueJu = skYueJuMapper.selectById(skYueJuUser.getYueJuId());
        if (skYueJu == null) {
            CssExceptionDescription.errorMsg(10020);
        }
        if (skYueJu.getState().longValue() == 0) {
            CssExceptionDescription.errorMsg(10021);
        }
        if (skYueJuUser.getState().longValue() == 3) {
            CssExceptionDescription.errorMsg(10022);
        }
        skYueJuUser.setState(2L);
        baseMapper.updateById(skYueJuUser);
    }

    /**
     * 解散群聊
     */
    @Override
    public void dissolveGroupChat(AppJieSanParams params, HttpServletRequest req) throws Exception {

        SkYueJu skYueJu = skYueJuMapper.selectById(params.getYuejuId());
        if (params.getType().equals("1")) {
            //解散群聊
            skYueJu.setIsGroup(2L);
            skYueJuMapper.updateById(skYueJu);
            GroupMember[] members = new GroupMember[]{new GroupMember().setId(skYueJu.getUserId() + "")};
            GroupModel group = new GroupModel()
                .setId(skYueJu.getId() + "")
                .setMembers(members);
            appRongYunUtil.dismiss(group);
        } else {
            //个人退出
            UpdateWrapper update = new UpdateWrapper();
            update.set("is_group", 0);
            update.eq("yue_ju_id", params.getYuejuId());
            update.eq("user_id", CssUtils.getUserId(req));
            baseMapper.update(null, update);
            GroupMember[] members = new GroupMember[]{new GroupMember().setId(CssUtils.getUserId(req) + "")};
            GroupModel group = new GroupModel()
                .setId(params.getYuejuId() + "")
                .setMembers(members);
            appRongYunUtil.quit(group);
        }
    }

    /**
     * 我的群聊
     */
    @Override
    public List<AppYueJuListReturn> getMyGroupChat(Long userId) {
        List<AppYueJuListReturn> list = baseMapper.getMyGroupChat(userId);
        return list;
    }

    /**
     * 群组成员列表
     */
    @Override
    public List<AppUserInfo> getGroupUserList(AppJoinYueJuParams app, Long userId) {
        SkYueJu skYueJu = skYueJuMapper.selectById(app.getYuejuId());
        SkAppUser skAppUser = skAppUserMapper.selectById(skYueJu.getUserId());
        List<AppUserInfo> list = baseMapper.getGroupUserList(app.getYuejuId());
        AppUserInfo appUserInfo = new AppUserInfo();
        appUserInfo.setNickName(skAppUser.getNickName());
        appUserInfo.setHeadImage(skAppUser.getHeadImage());
        appUserInfo.setUserId(skAppUser.getId());
        list.add(appUserInfo);
        return list;
    }

    /**
     * 参加约局的人
     *
     * @param id
     * @return
     */
    @Override
    public List<SkYueJuUserVo> getYueJuUserByYueJiId(Long id) {
        List<SkYueJuUserVo> list = baseMapper.getYueJuUserByYueJiId(id);
        return list;
    }

    /**
     * 群成员-踢人
     */
    @Override
    public void groupKickingPeople(AppYueJuGroupKickingPeople app, Long userId) throws Exception {
        //个人退出
        UpdateWrapper update = new UpdateWrapper();
        update.set("is_group", 0);
        update.eq("yue_ju_id", app.getYuejuId());
        update.eq("user_id", app.getUserId());
        baseMapper.update(null, update);
        GroupMember[] members = new GroupMember[]{new GroupMember().setId(app.getUserId() + "")};
        GroupModel group = new GroupModel()
            .setId(app.getYuejuId() + "")
            .setMembers(members);
        appRongYunUtil.quit(group);
    }

    /**
     * 群成员列表
     */
    @Override
    public void queryGroupUserList(AppJoinYueJuParams app) {
        try {
            GroupModel group = new GroupModel()
                .setId(app.getYuejuId() + "");
            appRongYunUtil.query(group);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
